package com.security.lasys.agent.plugin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

public class Conf {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    String configFilePath = null;//文件位置

    public static String DEFAULT_CONFIG_FILE = "./config/agent_config.xml";

    public static String hostIP;

    private ConfigPraser configPraser;

    private HashMap<String,String> sectionMap;

    public HashMap<String,HashMap<String,String>> configMap;

    private void readFile(String filename){
        configFilePath = Conf.DEFAULT_CONFIG_FILE;
        if (new File(filename).exists()){
            configFilePath = filename;
        }
        configPraser = new ConfigPraser(configFilePath);
    }

    private String getFromFile(String section,String option){
        String value = "";
        value = configPraser.read(section,option);
        value = stripValue(value);
        return value;
    }

    public HashMap<String, HashMap<String, String>> hitems()// put config.xml
    // into hash[][];
    {

        configMap = new HashMap<String, HashMap<String, String>>();

        for (int i = 0; i < configPraser.showTitles().size(); i++) {
            sectionMap = new HashMap<String, String>();
            for (int k = 0; k < configPraser.showNames(
                    configPraser.showTitles().elementAt(i)).size(); k++) {

                sectionMap.put(configPraser.showNames(
                        configPraser.showTitles().elementAt(i)).elementAt(k),
                        getFromFile(configPraser.showTitles().elementAt(i),
                                configPraser.showNames(
                                        configPraser.showTitles().elementAt(i))
                                        .elementAt(k)));
            }
            configMap.put(configPraser.showTitles().elementAt(i), sectionMap);

        }

        return configMap;
    }



    public Boolean set(String section, String option, String value) {
        configMap.get(section).put(option, value);
        return configPraser.set(section, option, value);
    }

    public Vector<String> getSections() {
        Vector<String> sections = new Vector<String>();
        Iterator<String> i = configMap.keySet().iterator();
        while (i.hasNext()) {
            sections.add((String) i.next());
        }
        return sections;

    }

    @SuppressWarnings("rawtypes")
    public Vector<String> getOptions(String section) {
        Vector<String> options = new Vector<String>();
        Iterator i = configMap.get(section).keySet().iterator();
        while (i.hasNext()) {
            options.add((String) i.next());
        }
        return options;
    }


    public String get(String section, String option) {
        if (this.hasSection(section) && this.hasOption(section, option)) {
            return configMap.get(section).get(option);
        }
        return null;
    }

    public boolean hasOption(String section, String option) {
        if (!configMap.containsKey(section)) {
            return false;
        } else if (!configMap.get(section).containsKey(option)) {
            return false;
        }
        return true;
    }

    public boolean hasSection(String section) {
        if (!configMap.containsKey(section)) {
            return false;
        } else
            return true;
    }

    public void add(String section, String Option, String value) {
        if (hasOption(section, Option)) {
            set(section, Option, value);
        } else {
            configPraser.add(section, Option, value);
            configMap.get(section).put(Option, value);
        }
    }

    public boolean remove(String section){
        if(hasSection(section)){
            configPraser.removeSection(section);
            configMap.get(section).clear();
            return true;
        }
        else
            return false;
    }

    private String stripValue(String value)// remove " and ' from string;
    {
        if (value == "")
            return value;

        if (value.charAt(0) == '\"' || value.charAt(0) == '\''
                || value.charAt(value.length() - 1) == '\"'
                || value.charAt(value.length() - 1) == '\'') {
            if (value.charAt(0) == '\"') {
                value = value.substring(1);
            }
            if (value.charAt(value.length() - 1) == '\"') {
                value = value.substring(0, value.length() - 1);
            }
            if (value.charAt(0) == '\'') {
                value = value.substring(1);
            }
            if (value.charAt(value.length() - 1) == '\'') {
                value = value.substring(0, value.length() - 1);
            }
        }
        return value;
    }


    public Conf(String configfile)
    {

        readFile(configfile);
        hitems();
    }
}
